import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, classification_report
from sklearn.metrics import (
    classification_report,
    confusion_matrix,
    ConfusionMatrixDisplay,
)
import matplotlib.pyplot as plt

# 模拟数据集
data = {
    "Temperature": [25, 30, 20, 18, 27, 15, 32, 24, 29, 22],
    "Humidity": [60, 70, 85, 90, 65, 95, 50, 75, 68, 88],
    "WindSpeed": [10, 15, 5, 8, 12, 20, 18, 9, 7, 14],
    "Pressure": [1013, 1010, 1005, 1000, 1012, 998, 1015, 1007, 1010, 995],
    "RainTomorrow": [0, 1, 1, 1, 0, 1, 0, 0, 0, 1],
}

df = pd.DataFrame(data)
X = df.drop("RainTomorrow", axis=1)
y = df["RainTomorrow"]

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)

print("准确率：", accuracy_score(y_test, y_pred))
print("\n分类报告：")
print(classification_report(y_test, y_pred))
new_data = np.array([[28, 60, 10, 1012]])  # 新的一天的气象数据
prediction = model.predict(new_data)

if prediction[0] == 1:
    print("明天可能下雨！☔")
else:
    print("明天大概率不会下雨。☀️")

print("训练集的准确度", model.score(X_train, y_train))
print("测试集的准确度", model.score(X_test, y_test))

print(classification_report(y_test, y_pred))

cm = confusion_matrix(y_test, y_pred)
print("Confusion Matrix:")
print(cm)
# plt.rcParams["font.sans-serif"] = ["SimHei"]
# plt.figure(figsize=(8, 6))
# disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=y)
# disp.plot(cmap="Blues")
# plt.title("学生成绩等级分类混淆矩阵")
# plt.show()
